package cn.stylefeng.guns.modular.milestone.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.milestone.entity.Milestone;
import cn.stylefeng.guns.modular.milestone.mapper.MilestoneMapper;
import cn.stylefeng.guns.modular.milestone.model.params.MilestoneParam;
import cn.stylefeng.guns.modular.milestone.model.result.MilestoneResult;
import cn.stylefeng.guns.modular.milestone.service.MilestoneService;
import cn.stylefeng.guns.modular.milestoneDemand.entity.MilestoneDemand;
import cn.stylefeng.guns.modular.milestoneDemand.service.MilestoneDemandService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 里程碑 服务实现类
 * </p>
 *
 * @author jalor
 * @since 2019-10-20
 */
@Service
public class MilestoneServiceImpl extends ServiceImpl<MilestoneMapper, Milestone> implements MilestoneService {

    @Autowired
    private MilestoneDemandService milestoneDemandService;

    @Override
    public void add(MilestoneParam param) {
        Milestone entity = getEntity(param);
        this.save(entity);

        List<Long> demandIdList = entity.getDemandIdList();
        if (null != demandIdList && demandIdList.size() > 0) {
            demandIdList.forEach(demandId -> {
                // 保存里程碑-需求关联
                MilestoneDemand milestoneDemand = new MilestoneDemand();
                milestoneDemand.setMilestoneId(entity.getId());
                milestoneDemand.setDemandId(demandId);
                milestoneDemandService.save(milestoneDemand);
            });
        }
    }

    @Override
    public void delete(MilestoneParam param) {
        this.removeById(getKey(param));

        // 删除之前的需求
        Map<String, Object> map = Maps.newHashMap();
        map.put("milestone_id", getKey(param));
        milestoneDemandService.removeByMap(map);
    }

    @Override
    public void update(MilestoneParam param) {
        Milestone oldEntity = getOldEntity(param);
        Milestone newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);

        // 删除之前的需求
        Map<String, Object> map = Maps.newHashMap();
        map.put("milestone_id", newEntity.getId());
        milestoneDemandService.removeByMap(map);

        List<Long> demandIdList = newEntity.getDemandIdList();
        if (null != demandIdList && demandIdList.size() > 0) {
            demandIdList.forEach(demandId -> {
                // 保存里程碑-需求关联
                MilestoneDemand milestoneDemand = new MilestoneDemand();
                milestoneDemand.setMilestoneId(newEntity.getId());
                milestoneDemand.setDemandId(demandId);
                milestoneDemandService.save(milestoneDemand);
            });
        }
    }

    @Override
    public MilestoneResult findBySpec(MilestoneParam param) {
        return null;
    }

    @Override
    public List<MilestoneResult> findListBySpec(MilestoneParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MilestoneParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MilestoneParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Milestone getOldEntity(MilestoneParam param) {
        return this.getById(getKey(param));
    }

    private Milestone getEntity(MilestoneParam param) {
        Milestone entity = new Milestone();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
